//
//  BuyAirTicketRequest.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-25.
//
//

#import "BuyAirTicketRequest.h"

@implementation BuyAirTicketRequest

- (id)initBuyTicket:(OrderAirInfo*)orderInfo
          userArray:(NSMutableArray*)userArray
          startData:(NSString*)startData
           Delegate:(id<ServerCommunicationDelegate>)delegate
{
    self = [super init];
    if (self) {
        self.requestName = @"BuyAirTicketRequest";
        self.delegate = delegate;
        
        self.url = PROTOCOL_ORDER_URL;
    
        //cis array
        NSMutableArray *cisArray = [[[NSMutableArray alloc] init]autorelease];
        for (int i = 0; i < [userArray count]; i++) {
            NSDictionary *useDict = [userArray objectAtIndex:i];
            
            NSMutableDictionary *cisDict = [NSMutableDictionary dictionary];
            [cisDict setObject:[self URLEncodedString:[useDict objectForKey:@"name"] ] forKey:@"cname"];
            if ([[useDict objectForKey:@"type"] isEqualToString:@"成人"]) {
                [cisDict setObject:@"1" forKey:@"ctype"];
            } else if ([[useDict objectForKey:@"type"] isEqualToString:@"儿童"]) {
                [cisDict setObject:@"2" forKey:@"ctype"];
            }
            else{
                [cisDict setObject:@"3" forKey:@"ctype"];
            }
            [cisDict setObject:[self URLEncodedString:@"身份证"] forKey:@"cctype"];
            [cisDict setObject:[useDict objectForKey:@"idnum"] forKey:@"ccardno"];
            [cisDict setObject:@"0" forKey:@"buycnt"];
            [cisDict setObject:@"0" forKey:@"largessCnt"];
            [cisDict setObject:orderInfo.saleprice forKey:@"tprice"];
            [cisDict setObject:orderInfo.fuelfee forKey:@"cfuel"];
            [cisDict setObject:orderInfo.buildfee forKey:@"cbuild"];
            
            [cisArray addObject:cisDict];
        }
        
        //sails array
        NSMutableArray *sailsArray = [[[NSMutableArray alloc] init]autorelease];
        NSMutableDictionary *sailsDict = [NSMutableDictionary dictionary];
        [sailsDict setObject:@"" forKey:@"airways"];
        [sailsDict setObject:orderInfo.fltno forKey:@"fltno"];
        [sailsDict setObject:orderInfo.sc forKey:@"sc"];
        [sailsDict setObject:orderInfo.ec forKey:@"ec"];
        NSArray *tempDate = [startData componentsSeparatedByString:@"/"];
        [sailsDict setObject:[NSString stringWithFormat:@"%@-%@-%@", [tempDate objectAtIndex:0],
                              [tempDate objectAtIndex:1],
                              [tempDate objectAtIndex:2]] forKey:@"fdate"];
        [sailsDict setObject:orderInfo.deptime forKey:@"stime"];
        [sailsDict setObject:orderInfo.arrtime forKey:@"etime"];
        [sailsDict setObject:@"" forKey:@"pty"];
        [sailsDict setObject:@"" forKey:@"cabin"];
        [sailsDict setObject:@"" forKey:@"pnr"];
        [sailsArray addObject:sailsDict];
        
        [self.headerJsonDict setObject:@"015004" forKey:@"TranCode"];
        [self.headerJsonDict setObject:@"20" forKey:@"TxRecordNum"];
        [self buildJsonRequest];
        
        //li dic
        NSMutableDictionary *liDict = [NSMutableDictionary dictionary];
        NSDictionary *useDict = [userArray objectAtIndex:0];
        [liDict setObject:[self URLEncodedString:[useDict objectForKey:@"contactname"]] forKey:@"lnm"];
        [liDict setObject:[useDict objectForKey:@"contactphone"] forKey:@"mobile"];
        [liDict setObject:@"" forKey:@"tel"];
        [liDict setObject:@"" forKey:@"email"];
        [liDict setObject:@"" forKey:@"laddr"];
        [liDict setObject:@"" forKey:@"remark"];
        [liDict setObject:@"" forKey:@"incname"];
        
        //oi dic
        NSMutableDictionary *oiDict = [NSMutableDictionary dictionary];
        int salePrice = [orderInfo.saleprice intValue];
        int buildPrice = [orderInfo.buildfee intValue];
        int fualPrice = [orderInfo.fuelfee integerValue];
        int allPrice = (salePrice + buildPrice + fualPrice) * [userArray count];
        int allBuildPrice = buildPrice * [userArray count];
        int allFualPrice = fualPrice * [userArray count];
        [oiDict setObject:[NSString stringWithFormat:@"%d", allPrice] forKey:@"allprice"];
        [oiDict setObject:[NSString stringWithFormat:@"%d", allFualPrice] forKey:@"faul"];
        [oiDict setObject:[NSString stringWithFormat:@"%d", allBuildPrice] forKey:@"build"];
        [oiDict setObject:@"0" forKey:@"derate"];
        [oiDict setObject:@"1" forKey:@"ftype"];
        [oiDict setObject:@"1" forKey:@"gtt"];
        [oiDict setObject:@"" forKey:@"sta"];
        [oiDict setObject:@"0" forKey:@"pt"];
        [oiDict setObject:[NSString stringWithFormat:@"%d", [userArray count]] forKey:@"tn"];
        
        //构造结构体
        NSMutableDictionary *xmlDict = [NSMutableDictionary dictionary];
        [xmlDict setObject:cisArray forKey:@"cis"];
        [xmlDict setObject:oiDict forKey:@"oi"];
        [xmlDict setObject:liDict forKey:@"li"];
        [xmlDict setObject:sailsArray forKey:@"sails"];
        
        [self.bodyJsonDict setObject:xmlDict forKey:@"strXml"];
        
        [self.headerJsonDict setObject:@"015004" forKey:@"TranCode"];
        [self.headerJsonDict setObject:@"20" forKey:@"TxRecordNum"];
        
        //银联支付价格
        [self.headerJsonDict setObject:[NSString stringWithFormat:@"%d", allPrice*100] forKey:@"TxAmt"];
        
        [self buildJsonRequest];

    }
    return self;
}


- (NSString*)URLEncodedString:(NSString*)input
{
    NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)input,
                                                                           NULL,
                                                                           CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                           kCFStringEncodingUTF8);
    [result autorelease];
    return result;
}

@end
